package com.mrd.gtimp.business.service;

import com.github.pagehelper.PageInfo;
import com.mrd.base.BaseBiz;
import com.mrd.gtimp.business.entity.Declaration;
import com.mrd.gtimp.business.entity.InspectionGoods;
import com.mrd.gtimp.business.repository.InspectionGoodsDao;
import com.mrd.gtimpframe.entity.DeclaCheckCtDeptRef;
import com.mrd.gtimpframe.entity.DeclaCheckCtItemRef;
import com.mrd.gtimpframe.entity.Dictionary;
import com.mrd.gtimpframe.repository.DeclaCheckContentMapper;
import com.mrd.gtimpframe.repository.DeclaCheckCtDeptRefMapper;
import com.mrd.gtimpframe.repository.DeclaCheckCtItemRefMapper;
import com.mrd.gtimpframe.service.DictionaryService;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2016/9/20.
 */
@Service
public class InspectionGoodsBiz extends BaseBiz<InspectionGoods> {
    @Resource
    @Qualifier("inspectionGoodsDao")
    @Override
    public void setBaseRepository(BaseRepository<InspectionGoods, String> baseRepository) {
        super.baseRepository = baseRepository;
    }

    @Resource
    private InspectionGoodsDao inspectionGoodsDao;

    @Resource
    private DeclaCheckContentMapper declaCheckContentMapper;

    @Resource
    private DeclaCheckCtDeptRefMapper declaCheckCtDeptRefMapper;

    @Resource
    private DeclaCheckCtItemRefMapper declaCheckCtItemRefMapper;

    @Resource
    private DictionaryService dictionaryService;

    public List<InspectionGoods> findByDeclaration(Declaration declaration) {
        return inspectionGoodsDao.findByDeclaration(declaration);
    }

    /**
     * 列表页面数据
     *
     * @param page   分页信息
     * @param params 查询参数
     */
    public Page<Map<String, Object>> listPageData(Pageable page, Map<String, Object> params) {
        List<Map<String, Object>> temp = new ArrayList<>();
        List<Map<String, Object>> pageMapList = declaCheckContentMapper.searchContent(params);
        for (Map<String, Object> stringObjectMap : pageMapList) {
            String contentId = (String) stringObjectMap.get("contentId");
            if (StringUtils.isNotEmpty(contentId)) {
                List<DeclaCheckCtDeptRef> deptRefList = declaCheckCtDeptRefMapper.selectListByContentId(contentId);
                List<DeclaCheckCtItemRef> itemRefList = declaCheckCtItemRefMapper.selectListByContentId(contentId);
                String deptStr = "";
                if (!CollectionUtils.isEmpty(deptRefList)) {
                    for (DeclaCheckCtDeptRef deptRef : deptRefList) {
                        Dictionary dictionary = dictionaryService.selectByPrimaryKey(deptRef.getItemId());
                        if (dictionary != null) {
                            String value = dictionary.getDictionaryName();
                            if (StringUtils.isNotEmpty(value) && value.equals("其他")) {
                                value = "其他：" + stringObjectMap.get("OTHER_INSPECT_DEPT");
                            }
                            deptStr += value + ",";
                        }
                    }
                    if (StringUtils.isNotEmpty(deptStr)) {
                        deptStr = deptStr.substring(0, deptStr.length() - 1);
                    }
                }
                String itemStr = "";
                if (!CollectionUtils.isEmpty(itemRefList)) {
                    for (DeclaCheckCtItemRef itemRef : itemRefList) {
                        Dictionary dictionary = dictionaryService.selectByPrimaryKey(itemRef.getItemId());
                        if (dictionary != null) {
                            String value = dictionary.getDictionaryName();
                            if (StringUtils.isNotEmpty(value) && value.equals("其他")) {
                                value = "其他：" + stringObjectMap.get("OTHER_INSPECT_PROJECT");
                            }
                            itemStr += value + ",";
                        }
                    }
                    if (StringUtils.isNotEmpty(itemStr)) {
                        itemStr = itemStr.substring(0, itemStr.length() - 1);
                    }
                }
                stringObjectMap.put("inspectionCompanyProject", deptStr + "&" + itemStr);
            }
            temp.add(stringObjectMap);
        }
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(temp);
        return new PageImpl<Map<String, Object>>(temp, page, pageInfo.getTotal());
    }
}
